﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;


public partial class jigou_list : System.Web.UI.Page
{

    string s_where, s_order;
    protected void Page_Load(object sender, EventArgs e)
    {
        Session["sTittle"] = "机构列表";

        string role = Session["sRole"].ToString().Trim();
        if (role != "管理员")
        {
            searchTr.Visible = false;
        }
        if (!IsPostBack)
        {
            DataBrid();
        }


    }

    //数据绑定
    private void DataBrid()
    {
        string strWhere = string.Empty;
        if (Session["sJigou"] == null)
        {
            Response.End();
        }
        string zjigou = Session["sJigou"].ToString().Trim();
        string role = Session["sRole"].ToString().Trim();
        switch (role)
        {
            case "学员":
            case "老师":
            case "机构":
                this.head_back.Visible = false;
                strWhere = " and jigou='" + zjigou + "'";
                break;
        }
        int record = 0;
        int count = 0;
        s_order = "ORDER BY [id] DESC";
        s_where = " where role='机构'";
        string sqlText = " [jigou], [id], [role], [username1], [city], [email], [mobile], [grjj],[zhuceRS],[yiskf]";
        DataTable DT = DbHelper.ExecutePage(sqlText, "[account]" + s_where + strWhere, "id", s_order, Pager.CurrentPageIndex, Pager.PageSize, out record, out count, null);
        Pager.RecordCount = record;
        this.GridView1.DataSource = DT;
        this.GridView1.DataBind();
    }




    //带搜索条件绑定
    private void DataBrid(string sqlwhere)
    {
        int record = 0;
        int count = 0;
        s_order = "ORDER BY [id] DESC";
        string sqlText = " [jigou], [id], [role], [username1], [city], [email], [mobile], [grjj],[zhuceRS],[yiskf]";
        DataTable DT = DbHelper.ExecutePage(sqlText, "[account]" + sqlwhere, "id", s_order, Pager.CurrentPageIndex, Pager.PageSize, out record, out count, null);
        Pager.RecordCount = record;
        this.GridView1.DataSource = DT;
        this.GridView1.DataBind();
    }

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)    //计算机构人数
    {
        Conn zconn = new Conn();

        string zjigou = e.Row.Cells[0].Text;//取得计数的机构名称
        //zsql = "select count(username),sum(yiskf) from account where role='学员' and jigou='" + zjigou + "'";
        if (zjigou != "" && zjigou != "&nbsp;")
        {
            var zsql = "select count(username) from account where jigou='" + zjigou + "'";
            string zcount = zconn.sqlFirst(zsql);
            zsql = "select sum(yiskf) from account where role='学员' and jigou='" + zjigou + "'";
            if (zcount != "0")
            {
                string mcount = zconn.sqlFirst(zsql);
                //zsql = "update account set zhuceRS=" + zcount.Substring(0, zcount.IndexOf("|")) + ",yiskf=" + zcount.Substring(zcount.IndexOf("|") + 1) + " where (((role = '机构') OR (role = '管理员')) AND (jigou='" + zjigou + "'))";
                zsql = "update account set zhuceRS=" + zcount + ",yiskf=" + mcount + " where (((role = '机构') OR (role = '管理员')) AND (jigou='" + zjigou + "'))";
                int noth = zconn.ExeSql(zsql);
                e.Row.Cells[6].Text = zcount;
            }
        }
        zconn.Dispose();
        e.Row.Cells[0].Visible = false;
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        string strWhere = " where role='机构'";
        if (!string.IsNullOrEmpty(txtKecheng1.Text))
        {
            strWhere += " and jigou like '%" + txtKecheng1.Text + "%'";
        }
        if (strWhere != " where role='机构'")
        {
            Pager.CurrentPageIndex = 0;
            DataBrid(strWhere);
            ViewState["sqlwhere"] = strWhere;
        }
        else
        {
            ViewState["sqlwhere"] = null;
            Pager.CurrentPageIndex = 0;
            DataBrid();
        }
    }

    protected void pager_PageChanging(object src, Wuqi.Webdiyer.PageChangingEventArgs e)
    {
        Pager.CurrentPageIndex = e.NewPageIndex;
        if (ViewState["sqlwhere"] != null)
        {
            DataBrid(ViewState["sqlwhere"].ToString());
        }
        else
        {
            DataBrid();
        }

    }
}
